load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")

sh_binary(
    name = "pgrepl-gen",
    srcs = ["//pkg/sql/lexbase:sql-gen.sh"],
)

# Define the target to auto-generate sql.go from the grammar file.
genrule(
    name = "pgrepl-goyacc",
    srcs = [
        "pgrepl.y",
    ],
    outs = ["pgrepl.go"],
    cmd = """
    export GOPATH=/nonexist-gopath
      $(location :pgrepl-gen) $(location pgrepl.y) pgrepl ""\
          $(location pgrepl.go) $(location @org_golang_x_tools//cmd/goyacc)

    """,
    tools = [
        ":pgrepl-gen",
        "@org_golang_x_tools//cmd/goyacc",
    ],
    visibility = ["//visibility:public"],
)

go_library(
    name = "pgreplparser",
    srcs = [
        "lexer.go",
        "parser.go",
        "pgrepl.go",
        "pgreplparser.go",
    ],
    importpath = "github.com/cockroachdb/cockroach/pkg/sql/pgrepl/pgreplparser",
    visibility = ["//visibility:public"],
    deps = [
        "//pkg/sql/lexbase",
        "//pkg/sql/parser",
        "//pkg/sql/parser/statements",
        "//pkg/sql/pgrepl/lsn",
        "//pkg/sql/pgrepl/pgrepltree",
        "//pkg/sql/pgwire/pgcode",
        "//pkg/sql/pgwire/pgerror",
        "//pkg/sql/sem/tree",
        "@com_github_cockroachdb_errors//:errors",
        "@com_github_cockroachdb_redact//:redact",  # keep
    ],
)

exports_files(
    [
        "pgrepl.y",
    ],
    visibility = ["//visibility:public"],
)

go_test(
    name = "pgreplparser_test",
    srcs = [
        "lexer_test.go",
        "parser_test.go",
    ],
    data = glob(["testdata/**"]),
    embed = [":pgreplparser"],
    deps = [
        "//pkg/sql/pgrepl/lsn",
        "//pkg/sql/pgwire/pgerror",
        "//pkg/sql/sem/tree",
        "//pkg/testutils/datapathutils",
        "@com_github_cockroachdb_datadriven//:datadriven",
        "@com_github_stretchr_testify//assert",
        "@com_github_stretchr_testify//require",
    ],
)
